import { getDetailListAPI, postCartListAPI } from '@/api/index.js'
export default {
  // 开启命名空间
  namespaced: true,
  state: {
    detailList: {},
    bigImg: '',
    goods: {}
  },
  mutations: {
    GETDETAILLIST(state, data) {
      state.detailList = data
    },
    GETBIGIMG(state, data) {
      state.bigImg = data
    },
    GETDETAILGOODS(state, data) {
      state.goods = data || {}
    }
  },
  actions: {
    async getDetailList({ commit }, skuId) {
      const data = await getDetailListAPI(skuId)
      commit('GETDETAILLIST', data)
      commit('GETBIGIMG', data.skuInfo.skuImageList[0].imgUrl)
    },
    async postCartList({ commit }, { skuId, skuNum }) {
      await postCartListAPI(skuId, skuNum)
    }
  },
  getters: {
    // computed属性调用时
    // 第一次state还没有数据读 {}
    // 第二次state有数据

    categoryView: state => {
      const { category1Name, category2Name, category3Name } = state.detailList.categoryView || {}
      return [category1Name, category2Name, category3Name]
    },
    price: state => state.detailList.price || {},
    spuSaleAttrList: state => state.detailList.spuSaleAttrList || {},
    skuInfo: state => state.detailList.skuInfo || {},
    skuImageList: (state) => state.detailList.skuInfo.skuImageList || {}

  }
}
